package com.fattyCorps.game.config;

import com.alibaba.fastjson.JSONObject;
import com.fattyCorps.game.obj.msg.HttpStatus;
import com.fattyCorps.game.service.GlobalService;
import com.fattyCorps.game.util.GameJwtUtils;
import com.fattyCorps.game.an.AMsgType;
import com.fattyCorps.game.cache.PlayerCache;
import com.fattyCorps.game.cache.PlayerMsgPipeMgr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@Component
public class CustomInterceptor implements HandlerInterceptor {

    @Autowired
    GlobalService globalService;

    @Autowired
    PlayerMsgPipeMgr playerMsgPipeMgr;

    @Autowired
    PlayerCache playerCache;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (globalService.serverState != GlobalService.SERVER_STATE.RUNNING){
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");

            JSONObject retJson = new JSONObject();
            retJson.put("code", HttpStatus.SERVER_CLOSE.getCode());
            retJson.put("msg", HttpStatus.SERVER_CLOSE.getMsg());
            PrintWriter writer = response.getWriter();
            writer.append(retJson.toString());
            return false;
        }

        if (handler instanceof HandlerMethod) {
            HandlerMethod hm = (HandlerMethod)handler;
            AMsgType methodAnnotation = hm.getMethodAnnotation(AMsgType.class);
            if (methodAnnotation == null || !methodAnnotation.readOnly()) {
                Integer playerId = GameJwtUtils.getPlayerIdByRequest(request, false);
                if (playerId > 0) {
                    try {
                        playerMsgPipeMgr.before(playerId);
                    }catch (Exception e) {
                        playerMsgPipeMgr.after(playerId);
                        throw e;
                    }
                }
            }
        }

        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod hm = (HandlerMethod)handler;
            AMsgType methodAnnotation = hm.getMethodAnnotation(AMsgType.class);
            if (methodAnnotation == null || !methodAnnotation.readOnly()) {
                Integer playerId = GameJwtUtils.getPlayerIdByRequest(request, false);
                if (playerId > 0) {
                    playerMsgPipeMgr.after(playerId);
                }
            }
        }
    }
}